import { HttpStatus } from "@nestjs/common";

export interface IResponseAPI {
  status: boolean;
  code: number;
  message: string;
  data?: any;
  timestamp: number;
  __info?: any;
}

const IResponse = new (class {
  fail(message?: string, code?: number, data?: any): IResponseAPI {
    const response = {
      status: false,
      code: code || HttpStatus.BAD_REQUEST,
      data: data || null,
      message: message || "未知错误",
      timestamp: Date.now(),
    };
    return this.build(response);
  }

  success(data: any, message?: string): IResponseAPI {
    const response = {
      status: true,
      code: HttpStatus.OK,
      data,
      message: message || "success",
      timestamp: Date.now(),
    };
    return this.build(response);
  }

  build(response: IResponseAPI): IResponseAPI {
    return response;
  }
})();

export default IResponse;
